﻿using System.Collections.Generic;
using System.Web.Mvc;
using Model;
using Repository;
using UI.Models;

namespace UI.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var repository = RepositoryManager.CreateProductRepository();
            var repositoryImg = RepositoryManager.CreateImageRepository();

            var objProduct = repository.GetProductHottest();
            var model = new ProductModel();
            if (objProduct != null)
            {
                model = new ProductModel()
                {
                    Products = GetNewest(),
                    Image = repositoryImg.GetImageByProductID(objProduct.Id),
                    ProductId = objProduct.Id,
                    Description = objProduct.Description,
                    IsHot = objProduct.IsHot,
                    Name = objProduct.Name,
                    Priority = objProduct.Priority
                };
                return View(model);
            }
            else
            {
                model = new ProductModel();
            }

            return View(model);
        }

        public List<ProductModel> GetNewest()
        {
            var repository = RepositoryManager.CreateProductRepository();
            var repositoryImg = RepositoryManager.CreateImageRepository();

            List<ProductModel> liProductModel = new List<ProductModel>();
            List<Product> liProduct = new List<Product>();
            liProduct = repository.GetProductNewest(3);
            if (liProduct.Count > 0)
            {
                for (int i = 0; i < liProduct.Count; i++)
                {
                    ProductModel objProduct = new ProductModel();
                    objProduct.Description = liProduct[i].Description;
                    objProduct.Image = repositoryImg.GetImageByProductID(liProduct[i].Id);
                    objProduct.IsHot = liProduct[i].IsHot;
                    objProduct.Name = liProduct[i].Name;
                    objProduct.Priority = liProduct[i].Priority;
                    objProduct.ProductId = liProduct[i].Id;
                    liProductModel.Add(objProduct);
                }
            }
            return liProductModel;
        }

        public ActionResult About()
        {
            var model = new ProductModel();
            return View(model);
        }

        public ActionResult Contact()
        {
            var model = new ProductModel();
            return View(model);
        }

        public ActionResult Employment()
        {
            var model = new ProductModel();
            return View(model);
        }
    }
}